import { xhrPostJson } from "@/services";

export type ajaxPost = (api: string, params: any) => Promise<any>;

type ExchRateFn = (crncy: string, baseCrncy: string) => void;
type BankInfoFn = (
  code: string,
  bankAcctType: string,
  defaultFlg: boolean
) => void;
/**
 * @author XJ
 *
 * @description  通过本位币、原币种 查询汇率
 * @param param0
 */

const handleExchRate: ExchRateFn = (crncy, baseCrncy) => {
  return xhrPostJson("/asm/rate/findOne", {
    currTypeF: crncy,
    currTypeT: baseCrncy,
  }).then((res) => {
    const { success }: any = res;
    return success ? res : {};
  });
};

/**
 * @author XJ
 *
 * @description 通过记账公司code，类型type查询银行信息
 * @param param0
 */
const handleBankInfo: BankInfoFn = (code, bankAcctType, defaultFlg) => {
  return xhrPostJson("/asm/bankAcct/list", {
    pageCode: "BankAcct_List",
    code,
    bankAcctType,
    defaultFlg,
  }).then((res) => {
    const { success }: any = res;
    return success ? res : {};
  });
};

const saveOrSubmit: ajaxPost = (api, params) => {
  return xhrPostJson(api, {...params}).then((res: any) => {
    if (res.success) {
      return true
    }
    return false
  }).catch(() => {
    return false
  })
}


export { handleExchRate, handleBankInfo, saveOrSubmit };
